//
//  CustomSliderView.m
//  汽车大灯色温蓝牙
//
//  Created by dfwx on 2020/9/5.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "CustomSliderView.h"

@implementation CustomSliderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setUpUI];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {


    [super drawRect:rect];
    self.number ++;
    if (self.number == 1) {
        self.backgroundColor = [UIColor clearColor];

        [self setUpUI];
    }

}

//- (void)setFrame:(CGRect)frame {
//
//    [super setFrame:frame];
//    [self setUpUI];
//
//
//}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.number = 0;
        self.backgroundColor = [UIColor clearColor];

//        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
    self.value = 0.0;
    self.backgroundColor = [UIColor clearColor];
//    self
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    
    CGFloat backgroundWidth = width -20;
    CGFloat backgroundHeight = backgroundWidth /193.5 *6.23;
    
    CGFloat thumWidth = backgroundHeight /6.23 *19.33;
    
    CGFloat showHeight = backgroundHeight *0.5;
    CGFloat showWidth = backgroundWidth *0.98;
    
    if (!self.backgroundImageView) {
        self.backgroundImageView = [[UIImageView alloc]init];
        self.backgroundImageView.frame = CGRectMake(10, (height-backgroundHeight)/2, backgroundWidth, backgroundHeight);
        self.backgroundImageView.image = [UIImage imageNamed:@"brightness_Bg"];
        self.backgroundImageView.userInteractionEnabled = YES;
        [self addSubview:self.backgroundImageView];
    }
    
    
    
    //TODO 添加进度条
    
    if (!self.showLabel) {
        self.showLabel = [[UILabel alloc]initWithFrame:CGRectMake(10+0.01*backgroundWidth, (height-showHeight)/2, 0, showHeight)];
        self.showLabel.backgroundColor = UIColorFromHex(0x3ABBDD);
        self.showLabel.layer.cornerRadius = showHeight/2.0;
        self.showLabel.layer.masksToBounds = YES;
        [self addSubview:self.showLabel];
    }
    
    if (!self.locationImageView) {
        self.locationImageView = [[UIImageView alloc]init];
        self.locationImageView.frame = CGRectMake(10-thumWidth/2.0+backgroundWidth *self.value, (height-thumWidth)/2, thumWidth, thumWidth);
        self.locationImageView.image = [UIImage imageNamed:@"brightness_Button"];
        self.locationImageView.userInteractionEnabled = YES;
        [self addSubview:self.locationImageView];
    }
    
    
    
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if (point.x >= 10 && point.x <= self.frame.size.width-10) {
        self.value = (point.x-10)/(self.frame.size.width-20);
        
    }else if (point.x < 10) {
        
        self.value = 0.0;
    }else {
        self.value = 1.0;
    }
//    if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderAction:)]) {
//        [self.delegate customSliderViewSliderAction:self.value];
//    }
//    [NSThread sleepForTimeInterval:0.3];
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderMoveAction:)]) {
//        [self.delegate customSliderViewSliderMoveAction:self.value];
//    }
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if (point.x >= 10 && point.x <= self.frame.size.width-10) {
        self.value = (point.x-10)/(self.frame.size.width-20);
        
    }else if (point.x < 10) {
        
        self.value = 0.0;
    }else {
        self.value = 1.0;
    }
    
    [self timerStart];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderStartAction:)]) {
        [self.delegate customSliderViewSliderStartAction:self.value];
    }
    
}

- (void)timerStart {
    
    [self timerEnd];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderAction:)]) {
            [self.delegate customSliderViewSliderAction:self.value];
        }
    }];
    
    
}

- (void)timerEnd {
    [self.timer invalidate];
//    self.timer = nil;
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    

    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if (point.x >= 10 && point.x <= self.frame.size.width-10) {
        self.value = (point.x-10)/(self.frame.size.width-20);
        
    }else if (point.x < 10) {
        
        self.value = 0.0;
    }else {
        self.value = 1.0;
    }
    
    [self timerEnd];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderEndLearnAction:)]) {
        [self.delegate customSliderViewSliderEndLearnAction:self.value];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customSliderViewSliderAction:)]) {
        [self.delegate customSliderViewSliderAction:self.value];
    }
    
    
}

- (void)setValue:(CGFloat)value {
    
    _value = value;
//    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    
    CGFloat backgroundWidth = width -20;
    CGFloat backgroundHeight = backgroundWidth /193.5 *6.23;
    
    CGFloat thumWidth = backgroundHeight /6.23 *19.33;
    CGRect rect = self.locationImageView.frame;
    rect.origin.x = 10-thumWidth/2.0+backgroundWidth *self.value;
    self.locationImageView.frame = rect;
    
    CGRect rect1 = self.showLabel.frame;
    rect1.size.width = backgroundWidth*0.98 *self.value;
    self.showLabel.frame = rect1;
}



@end
